<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
		$this->load->database();
	}



	function show_report_order_by_time($id,$date){
		$this->load->database();
		$query = $this->db->query("SELECT tbl_order_item.date_create, tbl_order_item.item_name, tbl_order_item.amount, (tbl_order_item.price * tbl_order_item.amount  ) AS total FROM tbl_oders INNER JOIN tbl_order_item ON tbl_oders.id = tbl_order_item.order_id WHERE tbl_oders.date_create =  '".$date."' AND user_id =".$id);

		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function total_price_by_month($id,$month){
		$this->load->database();

		$query = $this->db->query("SELECT tbl_order_item.date_create, sum(tbl_order_item.price * tbl_order_item.amount) AS total FROM tbl_order_item JOIN tbl_oders ON tbl_oders.id = tbl_order_item.order_id WHERE user_id =".$id." and tbl_order_item.date_create like '%".$month."%'");
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $rows){
				return  $rows->total;
			}
		}
		return $query->result();
	}
	function total_price_by_date($id,$date){
		$this->load->database();

		$query = $this->db->query("SELECT tbl_order_item.date_create, sum(tbl_order_item.price * tbl_order_item.amount) AS total FROM tbl_order_item JOIN tbl_oders ON tbl_oders.id = tbl_order_item.order_id WHERE user_id =".$id." and tbl_order_item.date_create='".$date."'");
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $rows){
				return  $rows->total;
			}
		}
		return $query->result();
	}

	function show_all_order(){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function get_name_table_by_orderid($orderid){
		$this->load->database();
		$this->db->where('id',$orderid);
		$query = $this->db->get('tbl_tables');
		$tablename = "";
		foreach($query->result() as $table){
			$tablename=  $table->table_name;
		}
		return $tablename;
	}
	function show_all_sub_order($order_id) {
		$this->load->database();
		$this->db->select("id");
		$this->db->where('root_order',$order_id);
		$query=$this->db->get('tbl_oders');
		// 		var_dump($this->db->last_query());
		if ($query->num_rows() > 0)
		{
			$result = array();
			foreach ($query->result() as $order){
				$result[]=$order->id;
			}
			return $result;
		}
		else {
			return $query->result();
		}
	}
	function get_root_order_name($root_order_id) {
		$this->load->database();
		$this->db->select("order_name");
		$this->db->where("id",$root_order_id);
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			$name = $query->result();
			return $name[0]->order_name;
		}
		else {
			return false;
		}
	}
	function show_username_this_order($orderid){
		$this->load->database();
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row){
				return $row->user_id;
			}
		}
		return $query->result();
	}

	function show_all_order_of_user($userid){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where("user_id", $userid);
		$this->db->where('status',0);
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			var_dump($query->result());die();

			//return $query->result();
		}
		return $query->result();
	}

	function finish_order($id){
		$this->load->database();
		$data = array(
				'status'=>1
		);

		$this->db->where('id', $id);
		$this->db->update('tbl_order_item', $data);
	}
        
        function update_amount($id,$amount){
		$this->load->database();
		$data = array(
				'amount'=>$amount
		);

		$this->db->where('id', $id);
		$this->db->update('tbl_order_item', $data);
	}
 
     function update_item_name($id,$item_id,$item_name){

		$this->load->database();
		$data = array(
				'item_id'=>$item_id,
				'item_name' => $item_name
		);

		$this->db->where('id', $id);
		$this->db->update('tbl_order_item', $data);
	}


	function finish_item($item_id,$order_id){
		$this->load->database();
		$data = array(
				'status'=>'1'
		);

		$this->db->where(array(
				'id'=>$item_id
		));
		$this->db->update('tbl_order_item', $data);
	}
	function show_all_orderitem_status(){
		$this->load->database();
		$this->db->where('date_create',date("d-m-Y"));
		$this->db->where('status',0);
		$this->db->order_by("time_create", "ASC");
		$query=$this->db->get('tbl_order_item');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	//Long
	function get_order_item(){
		$this->load->database();
		$this->db->select('id,order_name,user_id');
		if ($this->session->userdata('usertype') == 3) {
			$this->db->where("user_id",$this->session->userdata('userid'));
		}
		$this->db->where('date_create',date("d-m-Y"));
		$this->db->where('status',0);
		$this->db->order_by("time_create", "DESC");
		// 		var_dump($this->db);die();
		$query=$this->db->get('tbl_oders');
		$result = array();
		if ($query->num_rows() > 0)
		{
			$orders = $query->result();
			foreach ($orders as $order) {
				$this->db->where('order_id',$order->id);
				$this->db->where('date_create',date("d-m-Y"));
				$this->db->where('status',0);
				$this->db->order_by("time_create", "DESC");
				$sub_query = $this->db->get('tbl_order_item',2);

				if ($sub_query->num_rows() > 0) {
					$items =  $sub_query->result();
					$this->load->model('user_model');
					foreach ($items as $item) {
						$user = $this->user_model->load_details($order->user_id);
						if (count($user)>0) {
							$item->user_name = $user[0]->username;
						}
						else {
							$item->user_name = "Không rõ!";
						}
						$item->order_name = $order->order_name;
						$result[] = $item;
					}
				}
			}
		}

		usort($result, array('Order_model','cmp'));
		return $result;
	}
	function cmp($a, $b)
	{
		return strtotime($a->time_create) - strtotime($b->time_create);
	}
	function show_all_order_item($orderid){
		$this->load->database();
		$sub_order = $this->show_all_sub_order($orderid);
		$this->db->where('order_id',$orderid);
		if (count($sub_order)>0) {
			$this->db->or_where_in('order_id', $sub_order);
		}
		$query=$this->db->get('tbl_order_item');
		// 		var_dump($sub_order);
		// 		var_dump($this->db->last_query());die();
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function load_details($id){
		$this->load->database();
		$this->db->where("id",$id);
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	function load_multi_details($orderidArr){
		$this->load->database();
		$this->db->where_in("id",$orderidArr);
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	function add_orders($id_table,$name,$user_id){
		$this->load->database();
		$data = array(
				'table_id' =>$id_table,
				'order_name'=>$name,
				'status'=>0,
				'user_id'=>$user_id,
				'date_create'=>date("d-m-Y"),
				"time_create"=>date("H:i:s")

		);
		$this->db->insert('tbl_oders', $data);
		return $this->db->insert_id();
	}
	function add_root_order($oder_id, $root_order) {
		$this->load->database();
		$data = array(
				"root_order"=>$root_order
		);

		$this->db->where('id', $oder_id);
		$this->db->update('tbl_oders', $data);
	}
	function edit_order($id,$table_id,$order_name){
		$this->load->database();
		$data = array(
				'table_id'=>$table_id,
				'order_name'=>$order_name,
				'date_create'=>date("d-m-Y"),
				"time_create"=>date("H:i:s")

		);

		$this->db->where('id', $id);
		$this->db->update('tbl_oders', $data);
	}

	function update_checkout_order($id){
		$this->load->database();
		$data = array(
				'status'=>1,
				'date_create'=>date("d-m-Y"),
				"time_create"=>date("H:i:s")

		);

		$this->db->where('id', $id);
		$this->db->update('tbl_oders', $data);
	}

	function update_checkout_order_pending($id){
		$this->load->database();
		$data = array(
				'status'=>2,
				'date_create'=>date("d-m-Y"),
				"time_create"=>date("H:i:s")

		);

		$this->db->where('id', $id);
		$this->db->update('tbl_oders', $data);
	}


	function add_order_item($orderid,$item_id,$itemname,$itemprice,$amount){
		$this->load->database();
		$data = array(
				'order_id'=>$orderid,
				'item_id'=>$item_id,
				"item_name"=>$itemname,
				"price"=>$itemprice,
				"amount"=>$amount,
				'date_create'=>date("d-m-Y"),
				"time_create"=>date("H:i:s")

		);
		$this->db->insert('tbl_order_item', $data);

	}

	function edit_order_item($orderid,$item_id,$itemname,$itemprice,$amount){
		//echo $orderid."-".$item_id."-".$itemname."-".$itemprice."-".$amount; die();
		//$this->clear_all_order_item($orderid);
		$this->load->database();
		$data = array(
				'order_id'=>$orderid,
				'item_id'=>$item_id,
				"item_name"=>$itemname,
				"price"=>$itemprice,
				"amount"=>$amount,
				'date_create'=>date("d-m-Y"),
				"time_create"=>date("H:i:s")

		);
		$this->db->insert('tbl_order_item', $data);

	}
	function update_item_status($item_id) {
		$this->load->database();
		$data = array(
				'status'=>'1',
		);

		$this->db->where('id', $item_id);
		$this->db->update('tbl_order_item', $data);
	}
	function remove_item_id_in_order($item_id){
		$this->load->database();
		$this->db->where(array(
				'id'=>$item_id
		));
		$this->db->delete('tbl_order_item');
	}

	function clear_all_order_item($orderid){
		$this->load->database();
		$this->db->where(array(
				'order_id'=>$orderid
		));
		$this->db->delete('tbl_order_item');
	}
	function total_price_order($orderid){
		$this->load->database();

		$query = $this->db->query("select order_id,sum(price*amount) as total from tbl_order_item where order_id=".$orderid." group by order_id ");
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $rows){
				return  $rows->total;
			}
		}
		return 0;
	}


	function delete_order($id){
		$this->load->database();
		$this->db->where(array(
				'id'=>$id
		));
		$this->db->delete('tbl_oders');
	}

	function delete_order_item($id){
		$this->load->database();
		$this->db->where(array(
				'order_id'=>$id
		));
		$this->db->delete('tbl_order_item');
	}

	//Long
	function show_all_orderitem_by_filter($user_id, $date, $status){
		$this->load->database();
		if ($user_id > 0) {
			$this->db->where('user_id',$user_id);
		}
		if (empty($date)) {
			$this->db->where('date_create',date("d-m-Y"));
		}
		else {
			$this->db->where('date_create',$date);
		}
		if ($status != 100) {
			$this->db->where('status',$status);
		}
		$this->db->order_by("time_create", "ASC");
		$query=$this->db->get('tbl_oders');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
}
?>